<!-- /*
 * @Author: 谢力 843926058@qq.com
 * @Date:   2019-11-06
 * @Last Modified by:   谢力
 * @Last Modified time: 2019-11-07
 */ -->
<template>
    <div class="ui-mode-box flex-col">
        <div class="mt-tab-inner relative flex1">
            <div class="ui-mode-box body" ref="scroll">
                <div class="relative" v-if="user_quan_list&&user_quan_list.length">
                    <div class="coupon-wrap">
                        <div class="coupon-item"
                            :key="`coupon-${i}`"
                            v-for="(item,i) in user_quan_list">
                            <div class="coupon-info flex-row align-between-row align-end-row">
                                <div class="coupon-price flex-col align-center-row">
                                    <div class="coupon-minus"><span>¥</span>{{item.value_4_use_rule}}</div>
                                    <div>满{{item.key_4_use_rule}}元可用</div>
                                </div>
                                <div class="coupon-rules">
                                    <div class="coupon-name">{{item.score_remark}}</div>
                                    <div class="coupon-time">{{item.time_begin.split(' ')[0]}} 至 {{item.time_end.split(' ')[0]}} 可用</div>
                                </div>
                                <i class="check-icon" :class="item.checked?'checked':''" @touchstart.stop @click="changeCouponCheck(i)"></i>
                            </div>
                            <div class="coupon-tip flex-row align-center-row">
                                <p class="coupon-type">店铺券</p>
                                <p>适用范围：仅限指定店铺线上使用</p>
                            </div>
                        </div>
                    </div>
                    <div v-if="isScrolling" class="flex1 flex-row align-center scrolling-content relative">
                        <span class="icon">
                            <bsx-loading master-background="rgba(0,0,0,0)" box-background="rgba(0,0,0,0)" :size="35" :show-text="false"></bsx-loading>
                        </span>
                        <span class="text">努力加载中...</span>
                    </div>
                    <div v-else-if="isLoadEnded" class="flex1 flex-row align-center scrolling-content relative">已经到底啦~~</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import mixinCommon from '../../../mixins/common';
import mixinRequest from '../../../mixins/request';
export default {
    name:'cart/coupon/used',
    data(){
        return {
            public_agent_info:{},
            public_user_info:{},
            user_quan_list:[],
            selected_index:''
        };
    },
    mixins:[
        mixinCommon,
        mixinRequest
    ],
    components:{
    },
    computed:{
    },
    watch:{
        resourceLoaded:{
            handler(flag){
                flag&&this.$nextTick(this.init);
            },
            immediate:true
        }
    },
    created(){
    },
    mounted(){
    },
    methods:{
        // 初始化
        init(){
            this.getListInfo({
                flag:true,
                page:0
            });
        },
        // 获取列表数据
		getListInfo({flag,page,callback}){
            this.getPageInfo({
                name:'my.coupon',
                param:{
                    invoke_method:'score_list_4_one_user',
                    id_public_agent_info_list_like:this.$store.state.global.userInfo.id_public_agent_info,
                    pre_db_para_begin_idx:0,
                    pre_db_para_length_4_list:1000,
                    type_4_score_display:2,
                    pre_cmd_para_load_goods:false
                }
            }).then(({result,response})=>{
                var user_quan_list = [];
                for(var i=0; i<result.public_score_list.length; i++){
                    var endding_time = this.growtime(result.public_score_list[i].time_end);
                    var current_time = Date.parse(new Date());
                    result.public_score_list[i].checked=false;
                    if(result.public_score_list[i].is_online===0){
                        if(endding_time<current_time){
                        }else{
                            if(result.public_score_list[i].key_4_use_rule<=this.$route.query.amount){
                                user_quan_list.push(result.public_score_list[i]);//未过期且金额超过可使用金额
                            }
                        }
                    }
                }
                if(user_quan_list&&user_quan_list.length){
                    this.user_quan_list.push(...user_quan_list);
                    this.currentPage=page;
                    this.isLoadEnded=true;
                    callback&&callback();
                }else{
                    console.log(flag)
                    if(flag){
                        this.showResult('没有查询到相关数据');
                    }else{
                        this.showToast('没有查询到相关数据',false);
                    };
                    callback&&callback();
                };
            }).catch(({msg})=>{
                this.showMessage(msg);
            });
		},
        // 比较时间大小（跟现在相比）
        growtime:function(value){
            let last;
            let newlast=new Array();
            last = value.replace(/\s+/g,",").replace(/[-|:|/]/g,",").split(",");//改变时间格式
            //循环遍历，将携带0的日期返回原样
            for( var i in last){
                if(last[i].length>=4){
                    newlast.push(Number(last[i]));
                }else{
                    if(last[i][0]=="0"){
                        newlast.push(Number(last[i].replace(last[i][0],"")));
                    }else{
                        newlast.push(Number(last[i]));
                    };
                };
            };
            last = Date.parse(new Date(newlast[0],newlast[1]-1,newlast[2],newlast[3],newlast[4],newlast[5]));
            return last;
        },
        // 请求失败刷新
        handleRefresh(){
            this.init();
        }
    },
    beforeDestroy(){
    }
}
</script>
<style lang="less" scoped>
</style>
